
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.TimeZone;

/**
 * Servlet implementation class TomcatAmbiente
 */
public class TomcatAmbiente extends HttpServlet {
	private static final long serialVersionUID = 1L;
        
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TomcatAmbiente() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		PrintWriter out = response.getWriter();
		out.println("Entrou no doGet");
		
	    ServletContext context = getServletContext();
	    out.println("req.getServerName(): " + request.getServerName());
	    out.println("req.getServerPort(): " + request.getServerPort());
	    out.println("context.getServerInfo(): " + context.getServerInfo());
	    out.println("getServerInfo() name: " + getServerInfoName(context.getServerInfo()));
	    out.println("getServerInfo() version: " + getServerInfoVersion(context.getServerInfo()));
	    
	    out.println("\n");
	    
	    out.println("context.getAttributeNames():");
	    Enumeration<?> e = context.getAttributeNames();
	    while (e.hasMoreElements()) {
	      String name = (String) e.nextElement();
	      out.println("  context.getAttribute(\"" + name + "\"): " + context.getAttribute(name));
	    }		
	    
	    // Timezone
	    out.println("\n");
	    out.println("user.timezone = " + System.getProperty("user.timezone"));
	    out.println("TimeZone.getDefault = " + TimeZone.getDefault());
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		// TODO Auto-generated method stub

	    PrintWriter out = res.getWriter();
	    out.println("Entrou no doPOST");
	    
	    ServletContext context = getServletContext();
	    out.println("req.getServerName(): " + req.getServerName());
	    out.println("req.getServerPort(): " + req.getServerPort());
	    out.println("context.getServerInfo(): " + context.getServerInfo());
	    out.println("getServerInfo() name: " + getServerInfoName(context.getServerInfo()));
	    out.println("getServerInfo() version: " + getServerInfoVersion(context.getServerInfo()));
	    
	    out.println("\n");
	    
	    out.println("context.getAttributeNames():");
	    Enumeration<?> e = context.getAttributeNames();
	    while (e.hasMoreElements()) {
	      String name = (String) e.nextElement();
	      out.println("  context.getAttribute(\"" + name + "\"): " + context.getAttribute(name));
	    }
	    
	    // Timezone
	    out.println("\n");
	    out.println("user.timezone = " + System.getProperty("user.timezone"));
	    out.println("TimeZone.getDefault = " + TimeZone.getDefault());
		    

	}
	
	 private String getServerInfoName(String serverInfo) {
		    int slash = serverInfo.indexOf('/');
		    if (slash == -1)
		      return serverInfo;
		    else
		      return serverInfo.substring(0, slash);
	 }
	 
	private String getServerInfoVersion(String serverInfo) {
		// Version info is everything between the slash and the space
		int slash = serverInfo.indexOf('/');
		if (slash == -1)
			return null;
		int space = serverInfo.indexOf(' ', slash);
		if (space == -1)
			space = serverInfo.length();
		return serverInfo.substring(slash + 1, space);
	}

}
